package com.hbm.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hbm.commonutils.JwtUtils;
import com.hbm.educenter.entity.UcenterMember;
import com.hbm.educenter.entity.vo.LoginVo;
import com.hbm.educenter.entity.vo.RegisterVo;
import com.hbm.educenter.mapper.UcenterMemberMapper;
import com.hbm.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hbm.educenter.utils.MD5;
import com.hbm.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import com.hbm.educenter.utils.MD5;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-27
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String login(LoginVo loginVo) {
        //获取登录手机号和密码
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //手机号和密码非空判断
        if(StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new GuliException(20001,"手机号或密码不存在,登录失败");
        }

        //判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        System.out.println("--------------------");
        System.out.println("登录人信息："+mobileMember);

        //判断查询对象是否为空
        if(mobileMember == null) {//没有这个手机号
            throw new GuliException(20001,"没有这个手机号,登录失败");
        }

        //判断密码
        //因为存储到数据库密码肯定加密的
        //把输入的密码进行加密，再和数据库密码进行比较
        //加密方式 MD5
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())) {
            throw new GuliException(20001,"登录失败");
        }

        //判断用户是否禁用
        if(mobileMember.getIsDisabled()) {
            throw new GuliException(20001,"登录失败");
        }

        //登录成功
        //生成token字符串，使用jwt工具类
        //传mobileMember
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(), mobileMember.getNickname());
        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        //获取注册的数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        //非空判断
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"提交的数据有空值，注册失败");
        }

        //判断手机号是否重复，表里面存在相同手机号不进行添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        //UcenterMember ucenterMember = baseMapper.selectOne(wrapper);
//        if (!StringUtils.isEmpty(ucenterMember)){
//            throw new GuliException(20001,"已存在该手机号，注册失败");
//        }

        //查找数量更快
        Integer integer = baseMapper.selectCount(wrapper);
       if(integer>0){
           throw new GuliException(20001,"已存在该手机号，注册失败");
       }

        //判断验证码
        //获取redis验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        if (!redisCode.equals(code)){
            throw new GuliException(20001,"验证码错误，注册失败");
        }

        //数据添加数据库中
        UcenterMember finallUcenterMember = new UcenterMember();
        //BeanUtils.copyProperties(registerVo,finallUcenterMember);
        finallUcenterMember.setMobile(mobile);
        finallUcenterMember.setNickname(nickname);
        finallUcenterMember.setPassword(MD5.encrypt(password));//密码需要加密的
        finallUcenterMember.setIsDisabled(false);//用户不禁用
        finallUcenterMember.setAvatar("https://guliedu-hbm.oss-cn-beijing.aliyuncs.com/2021/08/25/0c4f34de526741efb5fa624c28791ff0-file.png");
        baseMapper.insert(finallUcenterMember);

    }

    //根据openid判断
    @Override
    public UcenterMember getOpenIdMember(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    //查询某一天注册人数
    @Override
    public Integer countRegisterDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
